package com.lab.network.model;

import com.lab.network.config.CachePolicyEnum;
import com.lab.network.config.MethodEnum;
import com.lab.network.config.ProtocolEnum;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by ak
 * API请求的通信相关的信息配置<br/>
 * &nbspHttpConnect头信息，默认为空，初始化方法赋默认值<br/>
 * &nbspURL重定向配置，默认为true<br/>
 * &nbsp计算cachekey时的黑名单，默认为空<br/>
 * <p/>
 * 初始化方法中对以下connect头属性已赋值<br/>
 * &nbspaddRequestHeader("Accept-Encoding", "gzip");<br/>
 * &nbspaddRequestHeader("Accept-Language", "zh-CN,zh;");<br/>
 * &nbspaddRequestHeader("Connection", "keep-alive");<br/>
 */
public class CtRequestProp {
    /**
     * 请求协议类型  (http、https)
     */
    protected ProtocolEnum protocol = ProtocolEnum.HTTP;

    /**
     * Method类型(GET、POST)
     */
    protected MethodEnum method = MethodEnum.GET;
    /**
     * 是否使用缓存
     */
    protected CachePolicyEnum cacheModel = CachePolicyEnum.RequestIgnoreCache;
    /**
     * retryTime 重试次数
     */
    protected int retryTime = 0;
    /**
     * autoRedirect 是否自动重定向
     */
    protected boolean autoRedirect = true;
    /**
     * connHeaders 加入到Connection的Header
     */
    protected Map<String, String> requestHeaders = new HashMap<String, String>();
    /**
     * 不参与生成cacheKey的参数列表
     */
    protected Set<String> cacheKeyBlackList = null;
    /**
     * 自定义缓存时间（单位：秒）
     */
    private int customerCacheTime = 0;

    public CtRequestProp() {
    }

    /**
     * 获取网络通信协议
     *
     * @return the protocol
     */
    public ProtocolEnum getProtocol() {
        return protocol;
    }

    /**
     * 设置网络通信协议
     *
     * @param protocol the protocol to set
     */
    public void setProtocol(ProtocolEnum protocol) {
        if (null == protocol) {
            return;
        }
        this.protocol = protocol;
    }

    /**
     * http请求method
     *
     * @return the method
     */
    public MethodEnum getMethod() {
        return method;
    }

    /**
     * http请求method
     *
     * @param method the method to set
     */
    public void setMethod(MethodEnum method) {
        if (null == method) {
            return;
        }
        this.method = method;
    }

    /**
     * 是否自动重定向
     *
     * @return the autoRedirect
     */
    public boolean isAutoRedirect() {
        return autoRedirect;
    }

    /**
     * 设置重定向状态
     *
     * @param autoRedirect the autoRedirect to set
     */
    public void setAutoRedirect(boolean autoRedirect) {
        this.autoRedirect = autoRedirect;
    }

    /**
     * @return 重试次数
     */
    public int getRetryTime() {
        return retryTime;
    }

    /**
     * 设置重试次数
     *
     * @param retryTime
     */
    public void setRetryTime(int retryTime) {
        this.retryTime = retryTime;
    }

    public void decreaseRetryTime() {
        this.retryTime -= 1;
    }

    /**
     * @return
     */
    public Map<String, String> getRequestHeaders() {
        return requestHeaders;
    }

    /**
     * @param requestHeaders
     */
    public void addRequestHeaders(Map<String, String> requestHeaders) {
        this.requestHeaders.putAll(requestHeaders);
    }

    /**
     * @param key
     * @param value
     */
    public void addRequestHeader(String key, String value) {
        this.requestHeaders.put(key, value);
    }


    public CachePolicyEnum getCacheModel() {
        return cacheModel;
    }

    /**
     * 设置本次API请求的缓存模式
     *
     * @param cacheModel 缓存调用策略
     */
    public void setCacheModel(CachePolicyEnum cacheModel) {
        this.cacheModel = cacheModel;
    }

    /**
     * 获取request请求计算cache主键的排除列表
     *
     * @return
     */
    public Set<String> getCacheKeyBlackList() {
        return cacheKeyBlackList;
    }

    /**
     * 设置request请求计算cache主键的排除列表
     *
     * @param cacheKeyBlackList
     */
    public void setCacheKeyBlackList(Set<String> cacheKeyBlackList) {
        this.cacheKeyBlackList = cacheKeyBlackList;
    }

    public int getCustomerCacheTime() {
        return customerCacheTime;
    }

    public void setCustomerCacheTime(int customerCacheTime) {
        this.customerCacheTime = customerCacheTime;
    }
}
